// @ts-ignore
/* eslint-disable */

import { request, type RequestOptions } from '@/utils/request';
import type { MaterialSearch, MaterialSearchOut, MaterialOut } from '../types/material';

/**
 * 获取物料列表
 * @param input 搜索参数
 * @returns 物料列表
 */
export async function materials(input: MaterialSearch, options?: RequestOptions) {
    return request<MaterialSearchOut>(`material/materials`, {
        method: 'POST',
        data: input,
        ...(options || {}),
    });
}

/**
 * 获取单个物料
 * @param id 物料ID
 * @returns 物料详情
 */
export async function get(id: string, options?: RequestOptions) {
    return request<MaterialOut>(`material/${id}`, {
        method: 'GET',
        ...(options || {}),
    });
}

/**
 * 创建/编辑物料
 * @param input 物料数据
 * @returns 操作结果
 */
export async function edit(input: MaterialOut, options?: RequestOptions) {
    return request<string>(`material/edit`, {
        method: 'POST',
        data: input,
        ...(options || {}),
    });
}

/**
 * 逻辑删除物料
 * @param id 物料ID
 * @returns 操作结果
 */
export async function logicDelete(id: string, options?: RequestOptions) {
    return request<string>(`material/${id}/logic-delete`, {
        method: 'POST',
        ...(options || {}),
    });
}

/**
 * 真实删除物料
 * @param id 物料ID
 * @returns 操作结果
 */
export async function realDelete(id: string, options?: RequestOptions) {
    return request<string>(`material/${id}/real-delete`, {
        method: 'POST',
        ...(options || {}),
    });
}

/**
 * 批量删除物料
 * @param ids 物料ID数组
 * @returns 操作结果
 */
export async function batchDelete(ids: string[], options?: RequestOptions) {
    return request<string>(`material/batch-delete`, {
        method: 'POST',
        data: { ids },
        ...(options || {}),
    });
}

// 导入相关类型定义
export interface MaterialImportData {
  materialCode: string;
  materialName: string;
  materialCategoryId?: string;
  specification?: string;
  unit?: string;
  isVulnerable: boolean;
  status: boolean;
}

export interface MaterialImportFailedItem {
  rowIndex?: number;
  materialCode?: string;
  materialName?: string;
  errorMessage: string;
}

export interface MaterialBatchImportResult {
  total: number;
  success: number;
  failed: number;
  failedItems: MaterialImportFailedItem[];
}

/**
 * 批量导入物料
 * @param importData 导入数据数组
 * @returns 导入结果
 */
export async function batchImport(importData: MaterialImportData[], options?: RequestOptions) {
    return request<MaterialBatchImportResult>('material/batch-import', {
        method: 'POST',
        data: importData,
        timeout: 500000,
        ...(options || {}),
    });
}

// Excel解析结果类型定义
export interface ExcelParseResult {
  success: boolean;
  message: string;
  data: {
    rowIndex: number;
    materialCode: string;
    hasImage: boolean;
    imageCount: number;
    imageBase64List: string[];
  }[];
}

// 物料图片导入相关类型定义
export interface MaterialImageImportData {
  materialCode: string;
  imageMd5: string;
}

export interface MaterialImageBatchImportRequest {
  items: MaterialImageImportData[];
}

/**
 * 解析Excel文件并提取图片
 * @param file Excel文件
 * @returns 解析结果
 */
export async function parseExcelWithImages(file: File, options?: RequestOptions) {
    const formData = new FormData();
    formData.append('file', file);
    
    return request<ExcelParseResult>('material/parse-excel-with-images', {
        method: 'POST',
        data: formData,
        timeout: 5000000,
        ...(options || {}),
    });
}

/**
 * 批量导入物料图片
 * @param request 图片导入请求
 * @returns 导入结果
 */
export async function batchImportImages(data: MaterialImageBatchImportRequest, options?: RequestOptions) {
    return request<MaterialBatchImportResult>('material/batch-import-images', {
        method: 'POST',
        data: data,
        timeout: 5000000,
        ...(options || {}),
    });
}